package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.YyBidding;
import com.ruoyi.gcgl.domain.YyBiddingitem;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.IYyBiddingService;
import com.ruoyi.gcgl.service.IYyBiddingitemService;
import com.ruoyi.gcgl.service.IYyProjectService;
import com.ruoyi.system.service.ISysDeptService;

/**
 * 投标管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Controller
@RequestMapping("/yy/bidding")
public class YyBiddingController extends BaseController
{
    private String prefix = "yy/bidding";

    @Autowired
    private IYyBiddingService sjBiddingService;
    @Autowired
    private IYyBiddingitemService sjBiddingitemService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IYyProjectService gcProjectService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysDeptService deptService;
    
    final String processCode = "sjtoubiao";

    @GetMapping()
    public String bidding()
    {
        return prefix + "/bidding";
    }

    /**
     * 查询投标管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyBidding sjBidding)
    {
        startPage();
        sjBidding.setDeleteFlag(1);
        List<YyBidding> list = sjBiddingService.selectSjBiddingList(sjBidding);
        return getDataTable(list);
    }

    /**
     * 导出投标管理列表
     */
    @Log(title = "投标管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyBidding sjBidding)
    {
    	sjBidding.setDeleteFlag(1);
        List<YyBidding> list = sjBiddingService.selectSjBiddingList(sjBidding);
        ExcelUtil<YyBidding> util = new ExcelUtil<YyBidding>(YyBidding.class);
        return util.exportExcel(list, "投标管理数据");
    }

    /**
     * 新增投标管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
//        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
//        mmap.put("approveid", configlis.get(0).getLevel1id());
//        mmap.put("approvename", configlis.get(0).getLevel1name());
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/add";
    }

    /**
     * 新增保存投标管理
     */
    @Log(title = "投标管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyBidding sjBidding){
    	
    	sjBidding.setOrderNo(ReturnUUID.getOrderNo("TB"));
    	sjBidding.setBiddingstatus(3);//状态1
    	sjBidding.setDeleteFlag(1);
    	sjBidding.setCreateAt(new Date());
    	sjBidding.setCreateUser(getLoginName());
    	sjBidding.setUpdateAt(new Date());
    	sjBidding.setUpdateUser(getLoginName());
    	sjBiddingService.insertSjBidding(sjBidding);
    	
    	sjBiddingitemService.deleteSjBiddingitemBy4BId(sjBidding.getId());
    	
    	if(sjBidding.getItems()!=null && sjBidding.getItems().size()>0){
    		List<YyBiddingitem> sjBiddingitemlis = sjBidding.getItems();
        	for(YyBiddingitem item:sjBiddingitemlis){
        		item.setBiddingId(sjBidding.getId());
        		item.setGcProjectid(sjBidding.getGcProjectid());
        		item.setStatus(0);
        		item.setDeleteFlag(1);
        		item.setCreateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateAt(new Date());
        		item.setUpdateUser(getLoginName());
        		sjBiddingitemService.insertSjBiddingitem(item);
        	}
    	}
    	
        return toAjax(1);
    }

    /**
     * 修改投标管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
    	YyBidding sjBidding = sjBiddingService.selectSjBiddingById(id);
        
    	YyBiddingitem item = new YyBiddingitem();
        item.setDeleteFlag(1);
        item.setBiddingId(sjBidding.getId());
        List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
        sjBidding.setItems(items);
        
        if(sjBidding.getZbFile()!=null && !sjBidding.getZbFile().equals("")){
        	String[] zlis = sjBidding.getZbFile().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zbFilevo", file);
        }else{
        	mmap.put("zbFilevo", "");
        }
        
        mmap.put("sjBidding", sjBidding);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/edit";
    }
    
    /**
     * 修改投标管理
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
    	YyBidding sjBidding = sjBiddingService.selectSjBiddingById(id);
        
    	YyBiddingitem item = new YyBiddingitem();
        item.setDeleteFlag(1);
        item.setBiddingId(sjBidding.getId());
        List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
        sjBidding.setItems(items);
        
        if(sjBidding.getZbFile()!=null && !sjBidding.getZbFile().equals("")){
        	String[] zlis = sjBidding.getZbFile().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zbFilevo", file);
        }else{
        	mmap.put("zbFilevo", "");
        }
        
        mmap.put("sjBidding", sjBidding);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/detail";
    }

    /**
     * 修改保存投标管理
     */
    @Log(title = "投标管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyBidding sjBidding)
    {
    	sjBidding.setUpdateAt(new Date());
    	sjBidding.setUpdateUser(getLoginName());
    	sjBiddingService.updateSjBidding(sjBidding);
    	
    	sjBiddingitemService.deleteSjBiddingitemBy4BId(sjBidding.getId());
    	
    	if(sjBidding.getItems()!=null && sjBidding.getItems().size()>0){
    		List<YyBiddingitem> sjBiddingitemlis = sjBidding.getItems();
        	for(YyBiddingitem item:sjBiddingitemlis){
        		item.setBiddingId(sjBidding.getId());
        		item.setGcProjectid(sjBidding.getGcProjectid());
        		item.setStatus(0);
        		item.setDeleteFlag(1);
        		item.setCreateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateAt(new Date());
        		item.setUpdateUser(getLoginName());
        		sjBiddingitemService.insertSjBiddingitem(item);
        	}
    	}
    	
        return toAjax(1);
    }
    
    /**
     * 修改保存投标管理
     */
    @Log(title = "投标管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(YyBidding sjBidding){
    	
    	sjBidding.setUpdateAt(new Date());
    	sjBidding.setUpdateUser(getLoginName());
    	sjBiddingService.updateSjBidding4Check(sjBidding);
    	
    	sjBiddingitemService.deleteSjBiddingitemBy4BId(sjBidding.getId());
    	
    	List<YyBiddingitem> sjBiddingitemlis = sjBidding.getItems();
    	for(YyBiddingitem item:sjBiddingitemlis){
    		item.setBiddingId(sjBidding.getId());
    		item.setGcProjectid(sjBidding.getGcProjectid());
    		item.setStatus(0);
    		item.setDeleteFlag(1);
    		item.setCreateAt(new Date());
    		item.setCreateUser(getLoginName());
    		item.setUpdateAt(new Date());
    		item.setUpdateUser(getLoginName());
    		sjBiddingitemService.insertSjBiddingitem(item);
    	}
    	
        return toAjax(1);
    }

    /**
     * 删除投标管理
     */
    @Log(title = "投标管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjBiddingService.deleteSjBiddingByIds(ids));
    }
    
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
        
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        
		return configlis;
    }
    
    //---------------------审批方法
    /**
     * level1
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyBidding obj = sjBiddingService.selectSjBiddingById(Long.parseLong(p.getBusinessKey().toString()));
        	YyBiddingitem item = new YyBiddingitem();
            item.setDeleteFlag(1);
            item.setBiddingId(obj.getId());
            List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
            obj.setItems(items);
        	if(obj.getZbFile()!=null && !obj.getZbFile().equals("")){
            	String[] zlis = obj.getZbFile().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zbFilevo", file);
            }else{
            	mmap.put("zbFilevo", "");
            }
        	
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getBmsprId());//部门审核人
            mmap.put("approvename", obj.getBmspr());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level1check";
    }
    
    /**
     * level2
     * @return
     */
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyBidding obj = sjBiddingService.selectSjBiddingById(Long.parseLong(p.getBusinessKey().toString()));
        	YyBiddingitem item = new YyBiddingitem();
            item.setDeleteFlag(1);
            item.setBiddingId(obj.getId());
            List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
            obj.setItems(items);
        	if(obj.getZbFile()!=null && !obj.getZbFile().equals("")){
            	String[] zlis = obj.getZbFile().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zbFilevo", file);
            }else{
            	mmap.put("zbFilevo", "");
            }
        	
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(obj.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel3id());
            mmap.put("approvename", configlis.get(0).getLevel3name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level2check";
    }
    
    /**
     * level3
     * @return
     */
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyBidding obj = sjBiddingService.selectSjBiddingById(Long.parseLong(p.getBusinessKey().toString()));
        	YyBiddingitem item = new YyBiddingitem();
            item.setDeleteFlag(1);
            item.setBiddingId(obj.getId());
            List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
            obj.setItems(items);
        	if(obj.getZbFile()!=null && !obj.getZbFile().equals("")){
            	String[] zlis = obj.getZbFile().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zbFilevo", file);
            }else{
            	mmap.put("zbFilevo", "");
            }
        	
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(obj.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel4id());
            mmap.put("approvename", configlis.get(0).getLevel4name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level3check";
    }
    
    /**
     * level3
     * @return
     */
    @GetMapping("/level4")
    public String level4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyBidding obj = sjBiddingService.selectSjBiddingById(Long.parseLong(p.getBusinessKey().toString()));
        	YyBiddingitem item = new YyBiddingitem();
            item.setDeleteFlag(1);
            item.setBiddingId(obj.getId());
            List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
            obj.setItems(items);
        	if(obj.getZbFile()!=null && !obj.getZbFile().equals("")){
            	String[] zlis = obj.getZbFile().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zbFilevo", file);
            }else{
            	mmap.put("zbFilevo", "");
            }
        	
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level4check";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyBidding obj = sjBiddingService.selectSjBiddingById(Long.parseLong(p.getBusinessKey().toString()));
        	YyBiddingitem item = new YyBiddingitem();
            item.setDeleteFlag(1);
            item.setBiddingId(obj.getId());
            List<YyBiddingitem> items = sjBiddingitemService.selectSjBiddingitemList(item);
            obj.setItems(items);
            
            if(obj.getZbFile()!=null && !obj.getZbFile().equals("")){
            	String[] zlis = obj.getZbFile().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zbFilevo", file);
            }else{
            	mmap.put("zbFilevo", "");
            }
        	mmap.put("obj", obj);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
        }
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	//所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
    	
        return prefix + "/modifycheck";
    }
}